home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / apps / cmapDoctor / cmapview / cmapview.c next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  11.1 KB  |  499 lines

  1. /*
  2.  * Copyright (C) 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /*----------------------------------------------------------------
  18.  * 
  19.  *  cmapview - scans and displays colormaps and installed visuals
  20.  *         
  21.  *  
  22.  *  06.94  John Magdziarz, Silicon Graphics, Inc.
  23.  *         Last edit  07.22.94
  24.  * 
  25.  *----------------------------------------------------------------*/
  26. #include <stdio.h>
  27. #include <X11/Xlib.h>
  28. #include <X11/Xutil.h>
  29. #include "XLayerUtil.h"
  30. #include <X11/keysym.h>
  31.  
  32.  
  33. char *installNames[] = {
  34.     "No",
  35.     "Yes",
  36. };
  37.  
  38. char *vtypeNames[] = {
  39.     "Underlay",
  40.     "Normal",
  41.     "Overlay",
  42.     "Popup",
  43. };
  44.  
  45.  
  46. char *vclassNames[] = {
  47.     "StaticGray",
  48.     "GrayScale",
  49.     "StaticColor",
  50.     "PseudoColor",
  51.     "TrueColor",
  52.     "DirectColor",
  53. };
  54.  
  55. Display *dpy;
  56. int screen;
  57. Window win;
  58. XWindowAttributes win_attributes;
  59. XVisualInfo vistemplate, *vinfo;
  60. int junk;
  61. Visual *defaultVisual;
  62. Visual *vis;
  63. XLayerVisualInfo template;
  64. XLayerVisualInfo *otherLayerInfo, *defaultLayerInfo;
  65. GC gc;
  66. XSetWindowAttributes attr;
  67.  
  68.  
  69. enum Vtype {underlay, normal, overlay, popup};
  70.  
  71. struct {
  72.     Colormap cmap;
  73.     int vclass;
  74.     enum Vtype vtype;
  75.     int depth;
  76.     int installed;
  77.     int numRefs;
  78.     int layer;
  79.     Visual *visual;
  80.     GC gc;
  81.     Window win;
  82.     int winIO;
  83. } allcmaps[1000];
  84.  
  85. int cmapCnt = 0;
  86. int cmapsInstalled = 0;
  87. int winCnt = 0;
  88. int winChoice = 0;
  89.  
  90.  
  91. enum Vtype identifyVisual(Visual *);
  92. void getWindowCmapInfo(Window);
  93.  
  94.  
  95. usage()
  96. {
  97.     printf("error in usage\n");
  98. }
  99.  
  100.       
  101. Display *OpenDisplay(display_name)
  102. char *display_name;
  103. {
  104.     Display *d;
  105.  
  106.     d = XOpenDisplay(display_name);
  107.     if (d == NULL) {
  108.         fprintf (stderr, "unable to open display '%s'\n",
  109.               XDisplayName (display_name));
  110.         usage ();
  111.         /* doesn't return */
  112.     }
  113.  
  114.     return(d);
  115. }
  116.  
  117.  
  118. Display_Window_Id(window, newline_wanted)
  119.     Window window;
  120.     Bool newline_wanted;
  121. {
  122.     char *win_name;
  123.     
  124.     printf("%ld", window);         /* print id # in hex/dec */
  125.  
  126.     if (!window) {
  127.     printf(" (none)");
  128.     } else {
  129.     if (window == RootWindow(dpy, screen)) {
  130.         printf(" (the root window)");
  131.     }
  132.     if (!XFetchName(dpy, window, &win_name)) { /* Get window name if any */
  133.         printf(" (has no name)");
  134.     } else if (win_name) {
  135.         printf(" \"%s\"", win_name);
  136.         XFree(win_name);
  137.     } else
  138.         printf(" (has no name)");
  139.     }
  140.  
  141.     if (newline_wanted)
  142.     printf("\n");
  143.  
  144.     return;
  145. }
  146.  
  147.  
  148. void
  149. buildWin(int newWin)
  150. {
  151.     attr.event_mask = ExposureMask;
  152.     attr.colormap = allcmaps[newWin].cmap;
  153.     attr.background_pixel = None;
  154.     attr.border_pixel = None;
  155.    
  156.     allcmaps[newWin].win = XCreateWindow(dpy, win,
  157.         0, 0, 256, 256,
  158.         0, allcmaps[newWin].depth, allcmaps[newWin].winIO, 
  159.         allcmaps[newWin].visual,
  160.         CWBorderPixel | CWBackPixel | CWColormap | CWEventMask, &attr);
  161.  
  162.     allcmaps[newWin].gc = XCreateGC(dpy, allcmaps[newWin].win, NULL, NULL);
  163. }
  164.  
  165.  
  166. void
  167. scanXTree(Window window, int recurse)
  168. {
  169.     walkTree(window, recurse, 0);
  170.     printf("\n\n");
  171. }
  172.  
  173.  
  174. walkTree(Window startWin, int recurse, int level)
  175. {
  176.   int i, j;
  177.   int rel_x, rel_y, abs_x, abs_y;
  178.   unsigned int width, height, border, depth;
  179.   Window root_win, parent_win;
  180.   unsigned int num_children;
  181.   Window *child_list;
  182.   XClassHint classhint;
  183.   int new;
  184.  
  185.   if (!XQueryTree(dpy, startWin, &root_win, &parent_win, &child_list,
  186.           &num_children)) {
  187.     fprintf(stderr, "Can't query window tree.");
  188.     exit(-1);
  189.   }
  190.  
  191.   if (level == 0)
  192.       printf("\nScanning window hierarchy...");
  193.  
  194.   if (level == 1) {
  195.     putc('.', stdout);
  196.     fflush(stdout);
  197.   }
  198.    
  199.   winCnt += num_children;
  200.  
  201.   getWindowCmapInfo(startWin);
  202.  
  203.   for (i = (int)num_children - 1; i >= 0; i--) {    
  204.     if (recurse)
  205.         walkTree(child_list[i], 1, level+1);
  206.   }
  207.   if (child_list) XFree((char *)child_list);
  208. }
  209.  
  210.  
  211. void
  212. getWindowCmapInfo(Window startWin)
  213. {
  214.   int j;
  215.   XWindowAttributes win_attributes;
  216.   XVisualInfo vistemplate;
  217.   XVisualInfo *vinfo;
  218.   int junk;
  219.   int newcmap;
  220.  
  221.  
  222.       if (!XGetWindowAttributes(dpy, startWin, &win_attributes)) {
  223.         fprintf(stderr, "Can't get window attributes.");
  224.         exit(-1);
  225.     }
  226.       vistemplate.visualid = XVisualIDFromVisual(win_attributes.visual);
  227.       vinfo = XGetVisualInfo(dpy, VisualIDMask, &vistemplate, &junk);
  228.  
  229.     newcmap = 1;
  230.     for (j = 0; j < cmapCnt; j++) 
  231.         if (win_attributes.colormap == allcmaps[j].cmap) {
  232.             allcmaps[j].numRefs++;
  233.             if (win_attributes.map_installed) 
  234.                 allcmaps[j].installed = True;
  235.             newcmap = 0;
  236.             break;
  237.         }
  238.         
  239.     if (newcmap && win_attributes.depth != 0) { /* if InputOnly, ignore */
  240.         allcmaps[cmapCnt].numRefs++;
  241.         allcmaps[cmapCnt].cmap = win_attributes.colormap;
  242.         allcmaps[cmapCnt].vclass = vinfo->class;
  243.         allcmaps[cmapCnt].visual = vinfo->visual;
  244.         allcmaps[cmapCnt].vtype = identifyVisual(vinfo->visual);
  245.         allcmaps[cmapCnt].depth = win_attributes.depth;
  246.         allcmaps[cmapCnt].winIO = win_attributes.class;
  247.         if (win_attributes.map_installed)
  248.             cmapsInstalled++;
  249.         else 
  250.             allcmaps[cmapCnt].installed = 0;
  251.         buildWin(cmapCnt);
  252.         cmapCnt++;
  253.     }
  254. }
  255.  
  256.  
  257. #if 0
  258.  
  259.   for (i = (int)num_children - 1; i >= 0; i--) {
  260.       if (!XGetWindowAttributes(dpy, window, &win_attributes)) {
  261.         fprintf(stderr, "Can't get window attributes.");
  262.         exit(-1);
  263.     }
  264.       vistemplate.visualid = XVisualIDFromVisual(win_attributes.visual);
  265.       vinfo = XGetVisualInfo(dpy, VisualIDMask, &vistemplate, &junk);
  266.  
  267.     new = 1;
  268.     for (j = 0; j < cmapCnt; j++) 
  269.         if (win_attributes.colormap == allcmaps[j].cmap) {
  270.             allcmaps[j].numRefs++;
  271.             if (win_attributes.map_installed) 
  272.                 allcmaps[j].installed = True;
  273.             new = 0;
  274.             break;
  275.         }
  276.         
  277.     if (new && win_attributes.depth != 0) {
  278.         allcmaps[cmapCnt].numRefs++;
  279.         allcmaps[cmapCnt].cmap = win_attributes.colormap;
  280.         allcmaps[cmapCnt].vclass = vinfo->class;
  281.         allcmaps[cmapCnt].visual = vinfo->visual;
  282.         allcmaps[cmapCnt].vtype = identifyVisual(vinfo->visual);
  283.         allcmaps[cmapCnt].depth = win_attributes.depth;
  284.         allcmaps[cmapCnt].winIO = win_attributes.class;
  285.         if (win_attributes.map_installed)
  286.             cmapsInstalled++;
  287.         else 
  288.             allcmaps[cmapCnt].installed = 0;
  289.         buildWin(cmapCnt);
  290.         cmapCnt++;
  291.     }
  292.     
  293.     if (recurse)
  294.     walkTree(child_list[i], 1, level+1);
  295.   }
  296.   if (child_list) XFree((char *)child_list);
  297. }
  298. #endif
  299.  
  300. enum Vtype
  301. identifyVisual(Visual *vis)
  302. {
  303.   int nVisuals;
  304.   static int defLayer;
  305.   int tmpLayer;
  306.   
  307.    if (!defaultVisual) { 
  308.      defaultVisual = DefaultVisual(dpy, screen);
  309.         template.vinfo.visualid = defaultVisual->visualid;
  310.         defaultLayerInfo = XGetLayerVisualInfo(dpy, VisualIDMask, &template, &nVisuals);
  311.      defLayer = defaultLayerInfo->layer;
  312.    }
  313.    
  314.    template.vinfo.visualid = vis->visualid; 
  315.    defaultLayerInfo = XGetLayerVisualInfo(dpy, 
  316.                  VisualIDMask, &template, &nVisuals);
  317.    tmpLayer = defaultLayerInfo->layer;
  318.    
  319.    if (tmpLayer < defLayer && defaultLayerInfo->type != None)
  320.       return(underlay);
  321.    else if (tmpLayer == defLayer)
  322.       return(normal);
  323.    else if (tmpLayer == defLayer + 1 && defaultLayerInfo->type != None)
  324.       return(overlay);
  325.    else if (tmpLayer == defLayer + 2 && defaultLayerInfo->type != None)
  326.       return(popup);
  327.    else {
  328.       fprintf(stderr, "error determining layer\n");
  329.        exit(-1);
  330.    }
  331. }
  332.  
  333.  
  334.  
  335. void
  336. listColormaps()
  337. {
  338.   int i;
  339.   char tmpstr[80];
  340.   
  341.   sprintf(tmpstr, "%9s%9s%10s%12s%9s%12s", "cmap", "depth", "vclass", "vtype", 
  342.               "# refs", "installed");
  343. /*
  344. tvclass\t\t# refs\tInstalled\n");
  345. */
  346.   printf("%s\n", tmpstr);
  347.   printf("---------------------------------------------------------------\n");
  348.  
  349.   for (i = 0; i < cmapCnt; i++) {
  350.       printf("%2d", i);
  351.        printf("%10d", allcmaps[i].cmap);
  352.       printf("%5d", allcmaps[i].depth);
  353.       printf("%15s", vclassNames[allcmaps[i].vclass]);
  354.       printf("%10s", vtypeNames[allcmaps[i].vtype]);
  355.        printf("%7d", allcmaps[i].numRefs);
  356.      printf("%10s", installNames[allcmaps[i].installed]);
  357.      printf("\n");
  358.   };
  359.   printf("\n\nClick MB1 in display window to cycle through colormaps that\n");
  360.   printf("are currently associated with windows on this server.\n\n");
  361. }
  362.  
  363.  
  364. void
  365. displayColormap(int winChoice)
  366. {
  367.   int i;
  368.   int sqw = 16;
  369.   int sqh = 16;
  370.   int cells;
  371.   int cellx;
  372.   int jump;
  373.   char title[80];
  374.   
  375.     cells = allcmaps[winChoice].visual->map_entries;
  376.     if (cells > 256)
  377.         cells = 256;
  378.     cellx = 16;
  379.     
  380.     XClearWindow(dpy, win);
  381.     XMapWindow(dpy, allcmaps[winChoice].win);
  382.     XClearWindow(dpy, allcmaps[winChoice].win);
  383.     sprintf(title, "%d)%s-%d-%s",
  384.                 winChoice, 
  385.                 vtypeNames[allcmaps[winChoice].vtype],
  386.                 allcmaps[winChoice].depth,
  387.                 vclassNames[allcmaps[winChoice].vclass]);
  388.                 
  389.     XStoreName(dpy, win, title);
  390.     for (i = 0; i < cells; i++) {
  391.         XSetForeground(dpy, allcmaps[winChoice].gc, i);
  392.         if (cells <= 256)
  393.             XFillRectangle(dpy, allcmaps[winChoice].win, allcmaps[winChoice].gc,
  394.                   (i % cellx) * sqw, (i / cellx) * sqh, sqw, sqh);
  395.         else
  396.             XDrawPoint(dpy, allcmaps[winChoice].win, allcmaps[winChoice].gc,
  397.                         (i % cellx), (i / cellx));
  398.     }
  399.     XFlush(dpy);
  400. }
  401.  
  402.  
  403. processEvents()
  404. {
  405.   XEvent event;
  406.   KeySym keysym;
  407.   char buf[4];
  408.   static int first = 1;
  409.  
  410.     while (XEventsQueued(dpy, QueuedAfterReading)) {
  411.     
  412.         XNextEvent(dpy, &event);
  413.         switch (event.type) {
  414.  
  415.         case Expose:
  416.             if (first) {
  417.                 first = 0;
  418.                 displayColormap(0);
  419.             }
  420.             else 
  421.                 displayColormap(winChoice);
  422.             break;
  423.         
  424.  
  425.         case MotionNotify:
  426.             break;
  427.  
  428.         case ButtonPress:
  429.           switch(event.xbutton.button) {
  430.  
  431.             case Button1:
  432.                 XUnmapSubwindows(dpy, win);
  433.                 winChoice++;
  434.                 winChoice = winChoice % cmapCnt;
  435.                 displayColormap(winChoice);
  436.                 break;
  437.  
  438.             case Button2:
  439.                 break;
  440.           }
  441.         break;
  442.         case KeyPress:
  443.         /* save out which unmodified key (i.e. the  key was
  444.                     *  not modified w/something like "Shift", "Ctrl",
  445.                     *  or "Alt") got pressed for use below.
  446.                     */
  447.         XLookupString((XKeyEvent *)&event, buf, 4, &keysym, 0);
  448.         if (keysym == XK_Escape)
  449.             exit();
  450.         break;
  451.         }
  452.     }
  453. }
  454.  
  455.  
  456.  
  457. void
  458. setWMColormaps()
  459. {
  460.   int i;
  461.   Window tmpwins[100];
  462.   
  463.   for (i = 0; i < cmapCnt; i++) 
  464.       tmpwins[i] = allcmaps[i].win;
  465.   XSetWMColormapWindows(dpy, win, tmpwins, cmapCnt);
  466. }
  467.  
  468.  
  469.  
  470. main()
  471. {
  472.     if (!(dpy = XOpenDisplay(NULL))) {
  473.         fprintf (stderr, "unable to open display '%s'\n",
  474.               XDisplayName(NULL));
  475.         usage ();
  476.     }
  477.     screen = DefaultScreen(dpy);
  478.  
  479.     attr.event_mask = KeyPressMask | ButtonPressMask | ExposureMask;
  480.     attr.background_pixel = BlackPixel(dpy, screen);
  481.     attr.border_pixel = None;
  482.  
  483.     win = XCreateWindow(dpy, RootWindow(dpy, screen),
  484.         0, 0, 256, 256,
  485.         0, 8, InputOutput, DefaultVisual(dpy, screen),
  486.         CWBorderPixel | CWBackPixel | CWEventMask, &attr);
  487.  
  488.     gc = XCreateGC(dpy, win, NULL, NULL);
  489.     XMapWindow(dpy, win);
  490.     XStoreName(dpy, win, "Dr. cmap\n");
  491.     XFlush(dpy);
  492.        scanXTree(RootWindow(dpy, screen), True);
  493.        setWMColormaps();
  494.     listColormaps();
  495.         
  496.     while (1)
  497.       processEvents();
  498. }
  499.